﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using System.Data;
using System.Data.SqlClient;
namespace QuantitativeInvestment.Factor
{
    class MoneyStrenghFactor:Factor
    {
        SqlConnection connection; // 数据库连接dd
        SqlCommand command; //数据库连接命

        public MoneyStrenghFactor()
        {
            this.name = "资金强度";

        }
        /// <summary>
        /// 给某只股票加入相应因子值
        /// </summary>
        /// <param name="stock"></param>
        /// <param name="para"></param>
        public override void addFactorValue(Stock stock)
        {
            string maxDate = this.dataModel.getMoneyStrengthMaxDate(stock.code);
            if (maxDate == "")
            {
                int startPostion = stock.tradingDateList.IndexOf("20070104");
                if (startPostion == -1)
                {
                    startPostion = stock.tradingDateList.IndexOf(stock.startDate);
                }
                int endPosition = stock.tradingDateList.IndexOf(stock.endDate);
                List<string> msTradingDateList = new List<string>();
                msTradingDateList = stock.tradingDateList.GetRange(startPostion, endPosition - startPostion);
                double[] ms = this.getMs(stock, msTradingDateList);
                this.dataModel.insertMoneyStrength(stock.code, ms, msTradingDateList);
            }
            else
            {
                if (Int32.Parse(maxDate) < Int32.Parse(stock.endDate))
                {
                    int maxDatePosition = stock.tradingDateList.IndexOf(maxDate);
                    int startPosition = maxDatePosition+1;
                    int endPosition = stock.tradingDateList.IndexOf(stock.endDate);
                    List<string> msTradingDateList = new List<string>();
                    msTradingDateList = stock.tradingDateList.GetRange(startPosition, endPosition - startPosition);
                    double[] ms = this.getMs(stock, msTradingDateList);
                    this.dataModel.insertMoneyStrength(stock.code, ms, msTradingDateList);
                }
                
            }

            if (!stock.factors.ContainsKey(this.name))
            {
                double[] msValues=this.dataModel.getMoneyStrengthList(stock.startDate,stock.endDate,stock.code);
                stock.factors.Add(this.name, msValues);

                Console.WriteLine(stock.code);
            }

        }

        private double[] getMs(Stock stock,List<string> tradingDateList)
        {
            List<double> msList = new List<double>();
            try
            {

                this.connection = new SqlConnection("Server=172.17.1.65; User=dfread; PWD=dfreadgta");
                this.connection.Open();
                this.command = this.connection.CreateCommand();

                string TABLENAME ="Taq_Data01min";
                for (int i = 0; i < tradingDateList.Count; i++)
                {
                    string databaseName = "GTA_TAQ" + tradingDateList[i].Substring(0, 6) + "210";
                    command.CommandText = "select clsprc,nvaltrd,ttime from " + databaseName + ".dbo." + TABLENAME + " where tdate='" + tradingDateList[i] + "' and " + "stkcd='" + stock.code + "' order by ttime";
                    SqlDataReader sdr = command.ExecuteReader();
                    double preClsprc = 0;
                    if (sdr.Read())
                    {
                        preClsprc = double.Parse(sdr["clsprc"].ToString());
                    }

                    double ms = 0;
                    while (sdr.Read())
                    {
                        double value = double.Parse(sdr["nvaltrd"].ToString());
                        double curClsprc = double.Parse(sdr["clsprc"].ToString());
                        if (curClsprc > preClsprc)
                            ms = ms + value;
                        else if (curClsprc < preClsprc)
                            ms = ms - value;
                        preClsprc = curClsprc;
                    }
                    msList.Add(ms);
                    sdr.Close();
                }
                this.connection.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return msList.ToArray();
            }
            return msList.ToArray();

       }


    }
}
